package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.Result;
import com.example.lotterysystem.common.utils.DateUtils;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.LotteryParam;
import com.example.lotterysystem.controller.param.WinningRecordListParam;
import com.example.lotterysystem.controller.result.WinningRecordListResult;
import com.example.lotterysystem.service.LotteryService;
import com.example.lotterysystem.service.dto.WinningRecordDTO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@RestController
public class LotteryController {

    @Autowired
    private LotteryService lotteryService;

    private ConcurrentHashMap<String, Long> map = new ConcurrentHashMap<>();
    public synchronized boolean checkUser(HttpServletRequest request, Long activityId){
        String user_token = request.getHeader("user_token");
        if (map.isEmpty()){
            map.put(user_token, activityId);
            return true;
        }
        if (map.containsKey(user_token)){
            if(!map.get(user_token).equals(activityId)){
                if(map.containsValue(activityId)){
                   return false;
                }
                map.put(user_token, activityId);
            }
            return true;
        } else {
            if(!map.containsValue(activityId)){
                map.put(user_token, activityId);
                return true;
            } else {
                return false;
            }
        }
    }
    @RequestMapping("/lottery/prize")
    public Result<Boolean> lotteryPrize(@Validated @RequestBody LotteryParam param, HttpServletRequest request){
        log.info("LotteryController lotteryPrize: {}", JacksonUtil.writeValueAsString(param));
        if(!checkUser(request, param.getActivityId())){
            return Result.success(false);
        }
        lotteryService.lotteryPrize(param);
        return Result.success(true);
    }

    @RequestMapping("/lottery/getWinningRecordList")
    public Result<List<WinningRecordListResult>> getWinningRecordList
            (@Validated @RequestBody WinningRecordListParam param){
        log.info("LotteryController getWinningRecordList: {}",
                JacksonUtil.writeValueAsString(param));
        List<WinningRecordDTO> winningRecordDTO = lotteryService.getWinningRecordList(param);
        List<WinningRecordListResult> resultList = winningRecordDTO.stream()
                .map(winningRecord -> {
                    WinningRecordListResult result = new WinningRecordListResult();
                    result.setWinnerId(winningRecord.getWinnerId());
                    result.setWinnerName(winningRecord.getWinnerName());
                    result.setPrizeName(winningRecord.getPrizeName());
                    result.setPrizeTier(winningRecord.getPrizeTier().getValue());
                    result.setWinningTime(DateUtils.dateFormat(winningRecord.getWinningTime()));
                    return result;
                }).toList();
        return Result.success(resultList);
    }
}
